package pers.bc.utils.yonyou;

import nc.bs.framework.common.InvocationInfo;
import nc.bs.framework.common.InvocationInfoProxy;
import pers.bc.utils.pub.LoggerUtil;

/**
 * 线程里面设置环境变量
 * @Author: Qingr
 * @Date: 2020-11-12 18:06
 */
public class InvocationInfoUtils
{
    private static volatile LoggerUtil logUtil = LoggerUtil.getInstance("yonylog");

    public static void setToken(byte[]token) { nc.bs.framework.comn.NetStreamContext.setToken(token); }
    
    public static byte[] getToken() { return nc.bs.framework.comn.NetStreamContext.getToken();}

    public static void setToken(String key,byte[]token) { nc.bs.framework.comn.NetStreamContext.setToken(key,token); }
    
    public static byte[] getToken(String key) { return nc.bs.framework.comn.NetStreamContext.getToken(key);}
    
     
    /**
     * *********************************************************** <br>
     * 说明： 指定用户初始化InvocationInfo<br>
     * @see <br>
     * @param pkUser sm_user 主键
     * @return 元数据表名 <br>
     * @String <br>
     * @methods pers.bc.utils.yonyou.YonYouUtilbc#getTable <br>
     * @author LiBencheng <br>
     * @date Created on 2020-11-18 <br>
     * @time 下午3:00:50 <br>
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static void initInvocationInfo(String pkUser) throws nc.vo.pub.BusinessException {
        try {
            // 取当前登录人
            String loginUser = nc.bs.framework.common.InvocationInfoProxy.getInstance().getUserId();
            byte[] logintoken = nc.bs.framework.comn.NetStreamContext.getToken();

            String dataSource = nc.bs.framework.common.InvocationInfoProxy.getInstance().getUserDataSource();
            // UserVO userVO =
            // NCLocator.getInstance().lookup(INCUserQueryService.class).findUserVO(dataSource,
            // userCode);
            nc.vo.sm.UserVO userVO = (nc.vo.sm.UserVO) YonYouUtilbc.getBaseDAO().retrieveByPK(nc.vo.sm.UserVO.class, pkUser);
            if (userVO != null) {
                nc.bs.framework.common.InvocationInfoProxy.getInstance().setUserDataSource(dataSource);
                nc.bs.framework.common.InvocationInfoProxy.getInstance().setGroupId(userVO.getPk_group());
                nc.bs.framework.common.InvocationInfoProxy.getInstance().setUserCode(userVO.getUser_code());
                nc.bs.framework.common.InvocationInfoProxy.getInstance().setUserId(userVO.getPrimaryKey());
                nc.bs.framework.server.ISecurityTokenCallback tc = YonYouUtilbc.NCLocator(nc.bs.framework.server.ISecurityTokenCallback.class);
                byte[] token = tc.token("__system".getBytes(),
                        nc.bs.framework.common.InvocationInfoProxy.getInstance().getUserCode().getBytes());
                nc.bs.framework.comn.NetStreamContext.setToken(token);
            } else {
                YonYouUtilbc.NCLocator (nc.bs.framework.server.ISecurityTokenCallback.class)
                .token("NCSystem".getBytes(), "#UAP#".getBytes());
            }
        } catch (Exception e) {
            logUtil.exception("initInvocationInfo异常：", e);
            throw new nc.vo.pub.BusinessException(e);
        }
    }
    
    public static InvocationInfo getInvocationInfo()
    {
        InvocationInfo info = new InvocationInfo();
        info.setBizCenterCode(InvocationInfoProxy.getInstance().getBizCenterCode());
        info.setBizDateTime(InvocationInfoProxy.getInstance().getBizDateTime());
        info.setBusiAction(InvocationInfoProxy.getInstance().getBusiAction());
        info.setGroupId(InvocationInfoProxy.getInstance().getGroupId());
        info.setGroupNumber(InvocationInfoProxy.getInstance().getGroupNumber());
        info.setUserDataSource(InvocationInfoProxy.getInstance().getUserDataSource());
        info.setUserId(InvocationInfoProxy.getInstance().getUserId());
        info.setLangCode(InvocationInfoProxy.getInstance().getLangCode());
        info.setLogLevel(InvocationInfoProxy.getInstance().getLogLevel());
        info.setHyCode(InvocationInfoProxy.getInstance().getHyCode());
        info.setSysid(InvocationInfoProxy.getInstance().getSysid());
        info.setDeviceId(InvocationInfoProxy.getInstance().getDeviceId());
        info.setRunAs(InvocationInfoProxy.getInstance().getRunAs());
        info.setCallId(InvocationInfoProxy.getInstance().getCallId());
        info.setCallServer(InvocationInfoProxy.getInstance().getCallServer());
        info.setUserCode(InvocationInfoProxy.getInstance().getUserCode());
        
        return info;
    }
    
    public static void setInvocationInfo(InvocationInfo invocationInfo)
    {
        if (invocationInfo == null)
        {
            return;
        }
        synchronized (InvocationInfoProxy.class)
        {
            InvocationInfoProxy.getInstance().setBizCenterCode(invocationInfo.getBizCenterCode());
            InvocationInfoProxy.getInstance().setBizDateTime(invocationInfo.getBizDateTime());
            InvocationInfoProxy.getInstance().setBusiAction(invocationInfo.getBusiAction());
            InvocationInfoProxy.getInstance().setGroupId(invocationInfo.getGroupId());
            InvocationInfoProxy.getInstance().setGroupNumber(invocationInfo.getGroupNumber());
            InvocationInfoProxy.getInstance().setUserDataSource(invocationInfo.getUserDataSource());
            InvocationInfoProxy.getInstance().setUserId(invocationInfo.getUserId());
            InvocationInfoProxy.getInstance().setLangCode(invocationInfo.getLangCode());
            InvocationInfoProxy.getInstance().setLogLevel(invocationInfo.getLogLevel());
            InvocationInfoProxy.getInstance().setHyCode(invocationInfo.getHyCode());
            InvocationInfoProxy.getInstance().setSysid(invocationInfo.getSysid());
            InvocationInfoProxy.getInstance().setDeviceId(invocationInfo.getDeviceId());
            InvocationInfoProxy.getInstance().setRunAs(invocationInfo.getRunAs());
            InvocationInfoProxy.getInstance().setCallId(invocationInfo.getCallId());
            InvocationInfoProxy.getInstance().setUserCode(invocationInfo.getUserCode());
        }
    }
}
